export default {
	async getShopCars() {
		let res = await this.httptool.post(this.apis.shopCarList, null);
		let tableData = res.data.list;
		for(let i = 0; i < tableData.length; i++) {
			tableData[i].checked = false;
			for(let j = 0; j < tableData[i].list.length; j++) {
				tableData[i].list[j].checked = false;
			}
		}
		this.tableData = tableData;
		this.setTotalMoney();
	},
	shopcarAdd() {
		this.checkProductQty(this, productId, qty, res => { //验证库存是否足够
			let params = {};
			params.productId = productId;
			params.qty = qty;
			params.buyNow = buyNow; //1为立即购买
			
			this.httptool.ajaxPost(this.apis.shopcarAdd, params, res => { //加入购物车
				
				if(buyNow == 1) { //如果是立即购买，判断是否有默认地址，有就跳转到确认订单页面
					this.httptool.ajaxPost(this.apis.getDefaultAddress, {}, resa => {
						let dataa = resa.data;
						this.$store.commit('changeValue', {
							name: 'confrimorder',
							value: {
								ids: res.data.id,
								address: dataa
							}
						});
						if(dataa && dataa.id) {
							this.$router.push('/orderconfirm');
						} else {
							this.$confirm('您还没有收货地址，赶紧去添加吧？').then(() => {
								this.$router.push('/addaddress?type=order');
							});
						}
					});
				}
				this.$refs.priceHeader.shopcarAPICount();
			});
		});

	},
	async shopcarDel(id) {
		let res = await this.httptool.post(this.apis.shopcarDel, {
			ids: id
		});
		this.$message.success('删除成功!')
//		this.getShopCars();
		this.$refs.priceHeader.shopcarAPICount();
		let tableData = this.tableData;
		for(let i = 0; i < tableData.length; i++) {
			for(let j = 0; j < tableData[i].list.length; j++) {
				if(tableData[i].list[j].id==id){
					this.tableData[i].list.splice(j,1);
					if(this.tableData[i].list.length==0){
						this.tableData.splice(i,1);
					}
					this.hanldeCheck(1,1);
					return;
				}
				
			}
		}
		
		
	},
	setTotalMoney() {
		let totalMoney = 0.00;
		for(let item of this.tableData) {
			for(let pro of item.list) {
				if(pro.checked && (pro.product.priceType != 'brent' && pro.product.priceType != 'wti')) {
					totalMoney += pro.product.price * pro.qty;
				}
			}
		}
		this.totalMoney = totalMoney.toFixed(2);
	},
	goPay() {
		let ids = '';
		let arr = []
		for(let item of this.tableData) {
			let temp_ids = item.list.filter(x => x.checked == true).map(x => x.id).join(',');
			if(temp_ids) {
				ids += "," + temp_ids;
			}
			for(let p of item.list) {
				if(p.checked == true) {
					arr.push({
						productId: p.product.id,
						qty: p.qty
					})
				}
			}
		}
		if(!ids) {
			this.$toast('请选择要购买的商品！');
			return false;
		}

		var checkParam = {};
		checkParam.list = arr;
		this.httptool.ajaxPost(this.apis.checkQty, checkParam, function(res) {
			this.httptool.ajaxPost(this.apis.getDefaultAddress, {}, res => {
				this.$store.commit('changeValue', {
					name: 'confrimorder',
					value: {
						ids: ids.substr(1),
						address: res.data
					}
				});
				if(!res.data) {
					this.$confirm('您还没有收货地址，赶紧去添加吧？').then(() => {
						this.$router.push('/addaddress?type=order');
					});
				} else {
					this.$router.push('/orderconfirm');
				}
			});
		});

	},
	checkProductQty() {
		let checkParam = {};
		checkParam.list = [];
		checkParam.list.push({
			productId: productId,
			qty: qty
		});
		this.httptool.ajaxPost(this.apis.checkQty, checkParam, successBack);
	}
}